﻿using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Description;
using System.Web.Mvc;
using MovieAPI.Models;

namespace MovieAPI.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*"), RequireHttpsAttribute]
    public class ActorController : ApiController
    {
        private filmEntities db = new filmEntities();

        // GET: api/Actor
        public IQueryable<ActorInfo> GetActorInfo()
        {
            return db.ActorInfo;
        }

        // GET: api/Actor/5
        [ResponseType(typeof(ActorInfo))]
        public IHttpActionResult GetActorInfo(int id)
        {
            ActorInfo actorInfo = db.ActorInfo.Find(id);
            if (actorInfo == null)
            {
                return NotFound();
            }

            return Ok(actorInfo);
        }

        // PUT: api/Actor/PutActorInfo
        [ResponseType(typeof(void))]
        public IHttpActionResult PutActorInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                int ID = Movie.ID;
                Actor actor = db.Actor.Where(M => M.ActorID == ID).FirstOrDefault();
                //MovieInfo.MovieAddress = Movie.Movie.MovieAddress;
                //MovieInfo.MovieName = Movie.Movie.MovieName;
                //MovieInfo.MovieDate = Movie.Movie.MovieDate;
                //MovieInfo.MovieImg = Movie.Movie.MovieImg;
                //MovieInfo.MovieTime = Movie.Movie.MovieTime;
                //MovieInfo.MovieType = Movie.Movie.MovieType;
                //MovieInfo.MovieReview = Movie.Movie.MovieReview;
                actor.ActorName = Movie.Movie.ActorName;
                actor.ActorImg = Movie.Movie.ActorImg;
                actor.Introduced = Movie.Movie.Introduced;
                actor.Birthday = Movie.Movie.Birthday;
                actor.Height = Movie.Movie.Height;
                actor.Nationality = Movie.Movie.Nationality;
                actor.Sex = Movie.Movie.Sex;
                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = actor.ActorID }, actor);
            }
        }

        // POST: api/Actor
        [ResponseType(typeof(Actor))]
        public IHttpActionResult PostActorInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                Actor actor = new Actor();
                actor.ActorName = Movie.Movie.ActorName;
                actor.ActorImg = Movie.Movie.ActorImg;
                actor.Introduced = Movie.Movie.Introduced;
                actor.Birthday = Movie.Movie.Birthday;
                actor.Height = Movie.Movie.Height;
                actor.Nationality = Movie.Movie.Nationality;
                actor.Sex = Movie.Movie.Sex;
                db.Actor.Add(actor);
                db.SaveChanges();

                return CreatedAtRoute("DefaultApi", new { id = actor.ActorID }, actor);
            }
        }

        // POST: api/MovieInfoes
        [ResponseType(typeof(Actor))]
        public IHttpActionResult AddMovieInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                Actor actor = new Actor();
                actor.ActorName = Movie.Movie.ActorName;
                actor.ActorImg = Movie.Movie.ActorImg;
                actor.Introduced = Movie.Movie.Introduced;
                actor.Birthday = Movie.Movie.Birthday;
                actor.Height = Movie.Movie.Height;
                actor.Nationality = Movie.Movie.Nationality;
                actor.Sex = Movie.Movie.Sex;
                db.Actor.Add(actor);
                db.SaveChanges();

                return CreatedAtRoute("DefaultApi", new { id = actor.ActorID }, actor);
            }
        }

        // DELETE: api/Actor/5
        [ResponseType(typeof(ActorInfo))]
        public IHttpActionResult DeleteActorInfo(int id)
        {
            ActorInfo actorInfo = db.ActorInfo.Find(id);
            if (actorInfo == null)
            {
                return NotFound();
            }

            db.ActorInfo.Remove(actorInfo);
            db.SaveChanges();

            return Ok(actorInfo);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ActorInfoExists(int id)
        {
            return db.ActorInfo.Count(e => e.ActorInfoID == id) > 0;
        }
        public IHttpActionResult GetActorByID(int id)
        {
            var actorInfo = db.ActorInfo.Where(A => A.ActorID == id).Include(a => a.Actor).Include(a => a.MovieInfo);
            return Ok(actorInfo.ToList());
        }
        public IHttpActionResult GetActorByMovieID(int id)
        {
            var actorInfo = db.ActorInfo.Where(A => A.MoiveID == id).Include(a => a.Actor).Include(a => a.MovieInfo);
            return Ok(actorInfo.ToList());
        }

        public IHttpActionResult GetAllActor()
        {
            using (filmEntities db = new filmEntities())
            {
                var actors = db.Actor.ToList();
                return Ok(actors);
            }
        }
        [System.Web.Http.HttpGet]
        public IHttpActionResult GetActorByActorID(int id)
        {
            using (filmEntities db = new filmEntities())
            {
                var actor = db.Actor.Where(a => a.ActorID == id).FirstOrDefault();
                return Ok(actor);
            }
        }
        public IHttpActionResult AddActAndDir(dynamic Info)
        {
            using (filmEntities db = new filmEntities())
            {

                ActorInfo actInfo = new ActorInfo();
                DirectInfo dirInfo = new DirectInfo();
                actInfo.ActorID = Info.ActID;
                actInfo.MoiveID = Info.MovieID;
                dirInfo.DirectID = Info.DirID;
                dirInfo.MovieID = Info.MovieID;
                db.ActorInfo.Add(actInfo);
                db.DirectInfo.Add(dirInfo);
                if (db.SaveChanges()==2)
                {
                    return Ok(1);
                }
                else
                {
                    return Ok(2);
                }
                
                
            }
        }
    }
}